package com.android.pdd.baseapp.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.text.TextUtils;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.android.pdd.baseapp.R;
import com.blankj.utilcode.util.ScreenUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.lg.meng.base.BaseDialog;

public class EditTextCallbackDialog extends BaseDialog {
    EditText editText;
    TextView btnCancel;
    TextView btnOk;

    public EditTextCallbackDialog(@NonNull Activity activity) {
        super(activity);
    }

    @Override
    protected int provideViewId() {
        return R.layout.dialog_edittext_callback;
    }

    @Override
    protected void initView() {
        editText = findViewById(R.id.et_dialog);
        if (!TextUtils.isEmpty(hint)) {
            editText.setHint(hint);
        }
        btnCancel = findViewById(R.id.btn_dialog_cancle);
        btnOk = findViewById(R.id.btn_dialog_ok);
        btnCancel.setOnClickListener(view -> {
            dismiss();
        });
        btnOk.setOnClickListener(view -> {
            String input = editText.getText().toString();
            if (TextUtils.isEmpty(input)) {
                ToastUtils.showLong("输入为空");
                return;
            }
            if (callback != null) {
                callback.onInput(input, EditTextCallbackDialog.this);
            }
            dismiss();
        });
    }

    @Override
    protected void initData() {
        setWidth(ScreenUtils.getScreenWidth() * 24 / 30);
    }


    public static EditTextCallbackDialog newInstance(Activity activity, Callback callback) {
        EditTextCallbackDialog dialog = new EditTextCallbackDialog(activity);
        dialog.callback = callback;
        return dialog;
    }

    private Callback callback;

    public interface Callback {
        void onInput(String text, Dialog dialog);
    }

    private String hint;

    public EditTextCallbackDialog hint(String hint) {
        this.hint = hint;
        return this;
    }
}
